/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ie.tcd.cs.railtickets.commons.util;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;

/**
 *
 * @author Wenbo Sun
 */
public class CacheUtil {

    private static CacheUtil instance;
    private static JCS cache;

    public CacheUtil() {
        try {
            cache = JCS.getInstance("cacheUtil");
        } catch (CacheException ex) {
            Logger.getLogger(CacheUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static CacheUtil getInstance() {
        synchronized (CacheUtil.class) {
            if (instance == null)
                instance = new CacheUtil();
        }

        return instance;
    }

    public void store(Object key, Object value) {
        try {
            if(cache.get(key) != null)
                cache.remove(key);
            cache.put(key, value);
        } catch (CacheException ex) {
            Logger.getLogger(CacheUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Object retreive(Object key) {
        return cache.get(key);
    }


}
